/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MainClasses.Event;

import MainClasses.*;
import MainClasses.FromAPCompSci.*;
import java.util.ArrayList;
/**
 *
 * @author andrewcutler
 */
public abstract class Event {
    
    public static int HOMEWORK   = 0;
    public static int CLASSWORK  = 1;
    public static int PROJECT    = 2;
    public static int TEST       = 3;
    public static int USER       = 4;
    public static int USERCHECK  = 5;
    public static int HOLIDAY    = 6;
    
    public static String[] typeStrings = {"HW","CW","PROJECT","TEST","USER","USER_CHECK","HOLIDAY"};
     
    private String name;
    private String description;
    private MyDate startDate;
    private MyDate endDate;
    private int type;
    
    public Event(int type)                           { init(type, null, null); }
    public Event(int type, String name)              { init(type, name, null); }
    public Event(int type, String name, String desc) { init(type, name, desc); }
    
    private void init(int type, String name, String description)    {
        this.type = type;
        this.name = name;
        this.description = description;
    }
    
    public void setName(String n)        { name = Event.removeForeignChars(n);        }
    public void setDescription(String d) { description = Event.removeForeignChars(d); }
    public void setStartDate(MyDate d)   { startDate = d;                             }
    public void setEndDate(MyDate d)     { endDate = d;                               }
    
    public int    getType()              { return type;                               }
    public String getName()              { return name;                               }
    public String getDescription()       { return description;                        }
    public MyDate getStartDate()         { return startDate;                          }
    public MyDate getEndDate()           { return endDate;                            }
    
    public String getUniqueID()    {
        return  typeStrings[getType()]+":::"
                +getName()+":::"
                +getEndDate().toString(MyDate.FORMAT_2, "-")+":::"
                +getStartDate().toString(MyDate.FORMAT_2, "-");                
    }
    
    public abstract EventPanel getPanel();
    
    public EventPanel getStandardPanel()
    {
        return new StandardPanel(this);
    }
    public boolean equal(String id)    {
        if(getUniqueID().equalsIgnoreCase(id))
        {    return true; }
        return false;
    }
    
    @Override
    public String toString(){ return name; }
    
    public static boolean checkable(Event e)    {
        if(e instanceof CheckableEvent)
            return true;
        return false;
                    
    }
    
    private static void addForeignChar(MyMap<String, String> list)    {
        list.put("&Agrave;","À");
        list.put("&agrave;","à");
        list.put("&Egrave;","È");
        list.put("&egrave;","è");
        list.put("&Igrave;","Ì");
        list.put("&igrave;","ì");
        list.put("&Ograve;","Ò");
        list.put("&ograve;","ò");
        list.put("&Ugrave;","Ù");
        list.put("&ugrave;","ù");
        
        list.put("&Aacute;","Á");
        list.put("&aacute;","á");
        list.put("&Eacute;","É");
        list.put("&eacute;","é");
        list.put("&Iacute;","Í");
        list.put("&iacute;","í");
        list.put("&Oacute;","Ó");
        list.put("&oacute;","ó");
        list.put("&Uacute;","Ú");
        list.put("&uacute;","ú");
        
        list.put("&Acirc;","Â");
        list.put("&acirc;","â");
        list.put("&Ecirc;","Ê");
        list.put("&ecirc;","ê");
        list.put("&Icirc;","Î");
        list.put("&icirc;","î");
        list.put("&Ocirc;","Ô");
        list.put("&ocirc;","ô");
        list.put("&Ucirc;","Û");
        list.put("&ucirc;","û");
        
        list.put("&Ccedil;","Ç");
        list.put("&ccedil;","ç");
        
        list.put("&Auml;","Ä");
        list.put("&auml;","ä");
        list.put("&Euml;","Ë");
        list.put("&euml;","ë");
        list.put("&Iuml;","Ï");
        list.put("&iuml;","ï");
        list.put("&Ouml;","Ö");
        list.put("&ouml;","ö");
        list.put("&Uuml;","Ü");
        list.put("&uuml;","ü");
        list.put("&#376;","Ÿ");
        list.put("&yuml;","ÿ");
        
        list.put("&Ntilde;","Ñ");
        list.put("&ntilde;","ñ");
        
        list.put("&laquo;", "<<");
        list.put("&raquo;", ">>");
        list.put("&lsquo;", "'");
        
        list.put("&rsquo;", "'");
        list.put("&ldquo;", "\"");
        list.put("&rdquo;", "\"");
        list.put("&iexcl;","¡");
        list.put("&iquest;","¿");
        list.put("&bull;", "\n ¬ ");
        list.put("&gt;", ">");
        list.put("&lt;", "<");
        list.put("&nbsp;", " ");
        list.put("<br />", "\n");
        list.put("<p>", " ");
        list.put("</p>", " ");
        
        
        
    }
    private static void addEscapeSequences(MyMap<String, String> list)    {
        list.put("\n", "<br /> ");
        list.put("\t", " ");
    }
                        
    
    public static String removeEscapeSequences(String s)
    {
        MyMap<String, String> chars = new MyMap<String, String>();
        addEscapeSequences(chars);
        ArrayList<MapEntry<String, String>> entries = chars.getEntries();
        
        for(MapEntry<String, String> entry:entries)
        {
            String key = entry.getKey();
            while(s.indexOf(key) >= 0)
            {
                int keyLength = key.length();
                String firstPart = s.substring(0, s.indexOf(key));
                String secondPart = s.substring(s.indexOf(key)+keyLength);
                if(s.indexOf(key) == 0)
                    s = secondPart;
                else
                    s = firstPart + entry.getValue() + secondPart;
            
            }
        }
        return s;
    }
    
    public static String removeForeignChars(String s)    {
        MyMap<String, String> foreignCharacters = new MyMap<String,String>();
        addForeignChar(foreignCharacters);
        //addEscapeSequences(foreignCharacters);
        ArrayList<MapEntry<String,String>> entries = foreignCharacters.getEntries();
        
        for(MapEntry<String,String> entry:entries)
        {
            String key = entry.getKey();
            while(s.indexOf(key) >= 0)
            {
                int keyLength = key.length();
                String firstPart = s.substring(0,s.indexOf(key));
                String secondPart = s.substring(s.indexOf(key)+keyLength);
                if(s.indexOf(key) == 0)
                    s = secondPart;
                else
                    s = firstPart + entry.getValue() + secondPart;
            }
            
        }
        
        return s;
    }
    
    

}
